package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppActivityOfferAndOrderBinding;
import com.sgb.kjwl.view.ui.adapter.transport.CargoManageHomeTabAdapter;
import com.sgb.kjwl.view.ui.fragment.transport.MyOfferListFragment;
import com.sgb.kjwl.view.ui.fragment.transport.MyOrderListFragment;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe:    我的报价/接单
 * Author:  yangLei
 * Date:    2021/11/1
 */
public class MyOfferAndOrderActivity extends BaseActivity<BaseViewModel, AppActivityOfferAndOrderBinding> {
    //顶部tab
    private CargoManageHomeTabAdapter mTabAdapter;
    private SlidingFragmentPagerAdapter mAdapter;
    private final String[] mTabs = new String[]{"我的报价", "我的接单"};

    @Override
    protected int layoutId() {
        return R.layout.app_activity_offer_and_order;
    }

    @Override
    protected void createViewModel() {

    }

    @Override
    protected void init() {
        mBinding.ivBack.setOnClickListener(view -> finish());
        initTab();
        initFragment();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MyOfferAndOrderActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("我的报价");
        tabs.add("我的接单");
        mTabAdapter = new CargoManageHomeTabAdapter(tabs);
        mBinding.rvTab.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mBinding.rvTab.setAdapter(mTabAdapter);
        mTabAdapter.setOnItemClickListener((adapter, view, position) -> {
            mTabAdapter.setSelectIndex(position);
            mBinding.vpMyOffer.setCurrentItem(position);
        });
    }

    private void initFragment() {
        //viewPager适配器
        ArrayList<Fragment> fragments = new ArrayList<>();
        //添加页面
        fragments.add(MyOfferListFragment.newInstance());
        fragments.add(MyOrderListFragment.newInstance());
        mAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), fragments, mTabs);
        mBinding.vpMyOffer.setAdapter(mAdapter);
        mBinding.vpMyOffer.setOffscreenPageLimit(2);
        mBinding.vpMyOffer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabAdapter.setSelectIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
